package us.icitap.timsportal.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import us.icitap.timsportal.entity.Module;
import us.icitap.timsportal.entity.OrgStructure;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.util.PortalNotification;
import us.icitap.timsportal.web.custom.ModuleButton;
import us.icitap.timsportal.web.custom.PortalLabel;
import us.icitap.timsportal.web.custom.PortalVerticalLayout;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;

@SuppressWarnings("serial")
public class ModuleMenu extends PortalVerticalLayout{
	private static final ThemeResource ICON = new ThemeResource("./icons/Apps-preferences-system-windows-icon.png");
	
	private Map<String, Button> buttonsMap = new HashMap<String, Button>();
	private ModuleButton moduleButton;
	
	public ModuleMenu(){
		setMargin(false, false, true, false);
		setSizeFull();
		PortalLabel title = new PortalLabel(TimsPortalLabels.getLabel("title.moduleBold"), Label.CONTENT_XHTML);
		title.setSizeFull();
		title.setStyleName("menu");
		addComponent(title);
		
	}
	
	@Override
	public void attach(){
		super.attach();
		List<Module> list = getApplication().getPortalBean().getListModule(false);
		System.out.println(list.size()+" getApplication().getPortalBean().getModuleList()");
				
		if(list!=null){
			for(Module m:list){
				moduleButton = new ModuleButton(m.getModuleName());
				moduleButton.setModule(m);
				moduleButton.setEnabled(getApplication().getSessionHandler().getModules().contains(m));
				moduleButton.setIcon(ICON);
				
				moduleButton.addListener(new Button.ClickListener(){
					@Override
					public void buttonClick(ClickEvent event) {

						ModuleButton btn = (ModuleButton) event.getSource();
						OrgStructure moduleStructure = getApplication().getSessionHandler().getUser().getBaseStructure();
						
						if(moduleStructure==null) {
							loop:
								for (String id : getApplication()
									.getSessionHandler().getUser().getOfficer()
									.getParentStructureIds()) {
								for(OrgStructure os: btn.getModule().getOrgStructures()){
									if(Integer.parseInt(id.trim()) == os.getId()){
										moduleStructure = os;
										break loop;
									}
								}
							}			
						}
						if(!btn.getModule().getOrgStructures().contains(moduleStructure)){
							PortalNotification.showWarning("Perdoruesi: "+getApplication().getSessionHandler().getUser().getUsername()
												+" nuk ka strukture organizative per modulin: "+btn.getModule().getModuleName(), getWindow());
						return;
						}
						
						
						boolean noError = getApplication().getSessionHandler().ridirectUserToModule(btn.getModule());
						if(!noError){
							getApplication().getSessionHandler().showErrorMessage();
							return;
						}
						
						/**
						 * PRODUCTION
						 * */
						String urlString = btn.getModule().getModuleURL()
						+ "?portalSessionId="
						+ getApplication().getSessionHandler()
								.getPortalSessionId();
						
						/**
						 * DEVELOPER
						 * */
//						String urlString = btn.getModule().getDevModuleURL()
//						+ "?portalSessionId="
//						+ getApplication().getSessionHandler()
//								.getPortalSessionId();

						ExternalResource resource = new ExternalResource(urlString);
						getApplication().getMainWindow().open(resource);
						
						WebApplicationContext webCtx = (WebApplicationContext)  getApplication().getContext();
				        HttpSession session = webCtx.getHttpSession();				        
				        
				        System.out.println("para:"+session.getId());
				        session.setMaxInactiveInterval(0);				        

					}
					
				});
				
				addComponent(moduleButton);
				setComponentAlignment(moduleButton, Alignment.MIDDLE_RIGHT);
				
				buttonsMap.put(m.getModuleName(), moduleButton);
			}				
		}
		
	}
}
